package cnn.user.controller;

import cnn.topfox.annotation.TokenOff;
import cnn.topfox.common.CommonException;
import cnn.topfox.common.Response;
import cnn.topfox.common.ResponseCode;
import cnn.topfox.data.TableInfo;
import cnn.topfox.misc.BeanUtil;
import cnn.topfox.misc.Misc;
import cnn.user.entity.UserDTO;
import cnn.user.entity.UserQTO;
import cnn.user.service.UserService;
import cnn.user.entity.SalaryDTO;
import cnn.user.entity.SalaryQTO;
import cnn.user.service.SalaryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/api/salary")
public class SalaryController {
    @Autowired
    SalaryService salaryService;
    @Autowired
    UserService userService;
    protected Logger logger = LoggerFactory.getLogger(getClass());

    @PostMapping
    public Response<SalaryDTO> insert(@RequestBody SalaryDTO dto) {
        salaryService.insert(dto);
        return new Response<>(dto, 1);
    }

    @PutMapping
    public Response<SalaryDTO> update(@RequestBody SalaryDTO dto) {
        int count = salaryService.update(dto);
        return new Response<>(dto, count);
    }

    @DeleteMapping("/{ids}")
    public Response<Integer> delete(@PathVariable("ids") Long ids) {
        Misc.checkObjNotNull(ids, "ids");
        int count = salaryService.deleteByIds(ids);
        return new Response<>(count);
    }
    @TokenOff
    @GetMapping
    public List<SalaryDTO> list(SalaryQTO qto) {
        return salaryService.listObjects(salaryService.select());
    }

    @TokenOff
    @GetMapping("/qto")
    public void qto() {
        // SalaryDTO 有双主键 setUserId setDeptId
        SalaryDTO dto1=salaryService.getObject(new SalaryQTO().setUserId(2).setDeptId("1"));
        SalaryDTO dto2=salaryService.getObject(new SalaryQTO().setUserId(2).setCreateUser("张三"));
        SalaryDTO dto3=salaryService.getObject(new SalaryQTO().setCreateUser("张三"));
        List<SalaryDTO> list = salaryService.listObjects(new SalaryQTO().setUserId(2).setDeptId("1"));

        //用户表 但主键
        UserDTO user1=userService.getObject(new UserQTO().setId("2").setName("张三"));
        UserDTO user2=userService.getObject("11");

        userService.listObjects(new UserQTO().setId("99").setName("qto").setOpenPage(false));

        userService.list(new UserQTO().setId("99").setName("qto").setOpenPage(false));

        userService.listObjects("1","2","3");
        List setIds=new ArrayList<>();
        setIds.add(1);
        setIds.add(22);
        setIds.add(333);
        userService.listObjects(setIds);

        salaryService.getObject("1-1");

        setIds.clear();
        setIds.add("11-1");
        setIds.add("12-2");
        setIds.add("13-3");
        salaryService.listObjects(setIds);
    }

    @TokenOff
    @GetMapping("/test")
    public void test() {
        SalaryDTO dto = new SalaryDTO().setUserId(2).setDeptId("1");
        BeanUtil.setValue(dto, TableInfo.get(SalaryDTO.class).getField("amount"),"111");
        salaryService.insert(dto);

        salaryService.delete(dto);
        salaryService.getObject(11);
        salaryService.deleteByIds(11);
        throw CommonException.newInstance(ResponseCode.DATA_IS_INVALID).text("异常");
    }

    /**
     * 多主键技巧一: 按多 Id 查询, 用减号- 隔离每个Id值
     * 输出SQL:
     * SELECT userId,deptId,amount,createUser,updateUser
     * FROM salary a
     * WHERE userId=1 and deptId=2
     */
    @TokenOff
    @GetMapping("/test1")
    public SalaryDTO test1() {
        return salaryService.getObject("1-2");
    }

    /**
     * 多主键技巧二: 多组Id值组合查询
     * 输出SQL:
     * SELECT userId,deptId,amount,createUser,updateUser
     * FROM salary a
     * WHERE ((userId=1 and deptId=1)
     *     OR (userId=1 and deptId=2)
     *     OR (userId=1 and deptId=3))
     */
    @TokenOff
    @GetMapping("/test2")
    public List<SalaryDTO> test2() {
        return salaryService.listObjects("1-1,1-2,1-3");
    }

    /**
     * 多主键技巧四: 按照id删除, 输出SQL
     *
     * SELECT userId,deptId,amount,createUser,updateUser
     * FROM salary a
     * WHERE ((userId=1 and deptId=1)
     *     OR (userId=1 and deptId=2))
     */
    @TokenOff
    @GetMapping("/test3")
    public void test3() {

        salaryService.deleteByIds("1-1,1-2");
    }
}